package com.fit.pages.user;

import java.util.List;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.dao.user.UserDAO;
import com.fit.entities.User;

@AdminAccess
public class ManageUsers {

	@Property
	private List<User> users;
	
	@Property
	private User user;
	
	@Inject
	private UserDAO userDAO;
	
	@Persist
	private String error;
	
	@Persist
	private String confirmation;
	
	void onActivate() {
		users = userDAO.findAll();
	}
	
	@OnEvent(component = "deleteLink")
	Object onDeleteLink(String username) {
		error="";
		confirmation="";
		try {
			userDAO.deleteByUsername(username);
			confirmation = "User \"" + username + "\" is deleted.";
			return null;
		} catch (Exception e) {
			error = "Error deleting user. ";
			return null;
		}
	}
	
	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation="";
		return null;
	}
	
	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error="";
		return null;
	}
	
	
	public boolean isErrorNotNull(){
		if(error==null || error==""){
			return false;
		}
		return true;
	}
	
	public boolean isConfirmationNotNull(){
		if(confirmation==null || confirmation==""){
			return false;
		}
		return true;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getConfirmation() {
		return confirmation;
	}

	public void setConfirmation(String confirmation) {
		this.confirmation = confirmation;
	}
	
	public List<User> getUsersReport() {
		return userDAO.findAll();
		
	} 


	
}
